﻿using System;
using System.Collections;
using System.Windows.Data;

namespace SearchForWorkitemsChanges
{
    /// <summary>
    /// Class implementing <see cref="IValueConverter"/> that will return you a bool value depending that the IEnumerable source contains or not items.
    /// Default returned value are : True if Items, False otherwise.
    /// No ConvertBack implemented.
    /// </summary>
    [ValueConversion(typeof(IEnumerable), typeof(bool))]
    internal class IEnumerableToBoolConverter : IValueConverter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="IEnumerableToBoolConverter" /> class.
        /// </summary>
        public IEnumerableToBoolConverter()
        {
            this.HasItemsValue = true;
            this.HasNoItemsValue = false;
        }


        /// <summary>
        /// Gets or sets a value indicating whether the IEnumerable source value has items.
        /// </summary>
        public bool HasItemsValue { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the IEnumerable source value has no items.
        /// </summary>
        public bool HasNoItemsValue { get; set; }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return this.HasNoItemsValue;

            if (value is IEnumerable)
            {
                var iterator = ((IEnumerable)value).GetEnumerator();
                if (iterator.MoveNext())
                    return this.HasItemsValue;
            }

            return this.HasNoItemsValue;
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
